<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//团队管理
class Team extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/teamModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$filter = array('name','company_id');
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->load->dmodel('team_region','team_region');
		$list = $result['list'];
		
		
		$this->load->model('User/companyModel','companyModel');
		
		
		
		foreach($list as $key=>$val){
			$members = explode(',',$val['members']);
			foreach($members as $k=>$v){
				if(empty($v)){
					unset($members[$k]);
				}
			}
			$list[$key]['member_num'] = count($members);
			
			$region_list = $this->team_region->getAll(array('team_id'=>$val['id']));
			$regions = array();
			
			foreach($region_list as $v){
				$regions[] = $v['name'];
			}
			
			$list[$key]['regions'] = implode(',',$regions);

			
			$company = $this->companyModel->getRow(array('id'=>$val['company_id']));
			$list[$key]['company_name'] = $company?$company['name']:'';
		}
		$result['list'] = $list;
		
		
		$this->data_out($result);
	}
	
	function info_out($info){
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		if(!$this->check_priv('team_update|team_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array(
			'name'=>'请输入名称',
			'company_id'=>'请选择所属公司'
		);
		$post = array('name','company_id');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		
		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("保存团队【{$result['id']}】信息",'team');
		$this->data_out($result);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('team_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		
		$this->add_log("删除团队【{$result}】",'team');
		$this->data_out($result);
		
	}
	//start
	/**
	* @name 添加成员
	* @param team_id ture int 团队ID
	* @param ids ture array|string 成员ID，逗号隔开
	* @return 成功返回1
	*/
	//end
	function add_member(){
		
		if(!$this->check_priv('team_update')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$ids = $this->input->post('ids',null,array());
		$team_id = $this->input->post('team_id',null,0);
		
		$arr = array();
		foreach($ids as $val){
			if(!empty($val)){
				$arr[] = $val;
			}			
		}
		
		$this->CurModel->edit(array('members'=>implode(',',$arr)),"id='{$team_id}'");
		
		
		$this->add_log("团队【{$team_id}】添加成员【".implode(',',$arr)."】成功",'team');
		$this->data_out(1);
		
	}
	//start
	/**
	* @name 获取成员
	* @param team_id ture int 团队ID
	* @return 成功返回1
	*/
	//end
	function get_member_list(){
		
		$team_id = $this->input->post('team_id',null,0);
		
		$info = $this->CurModel->get_info($team_id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->load->model('User/UserModel','UserModel');
		$list = $this->UserModel->ids_get_list($info['members']);
		if(!$list){
			$list = array();
		}
		$this->data_out($list);
	}
	//start
	/**
	* @name 删除成员
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del_member(){
		
		
		$id = $this->input->post('id',null,0);
		$this->load->model('User/teamMemberModel','teamMemberModel');
		
		$info = $this->teamMemberModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		$this->teamMemberModel->del("id='{$id}'");
		
		$this->add_log("删除团队成员【{$id}】",'team');
		$this->data_out(1);
		
	}
	
	function get_region_list(){
		
		$team_id = $this->input->post('team_id',null,0);
		
		$this->load->dmodel('team_region','team_region');
		$arr = array('team_id'=>$team_id);
		
		$result = $this->team_region->getAll(array('team_id'=>$team_id));
		$this->data_out($result);
	}
	
	function add_region(){
		
		
		
		$team_id = $this->input->post('team_id',null,0);
		$name = $this->input->post('name',null,'');
		
		$this->load->dmodel('team_region','team_region');
		$arr = array(
			'team_id'=>$team_id,
			'name'=>$name 
		);
		$is_exist = $this->team_region->_where_exist(array('team_id'=>$team_id,'name'=>$name));
		if($is_exist){
			$this->err_out('数据已存在，不可重复添加');
		}
		$result = $this->team_region->add($arr);
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 删除成员
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del_region(){
		
		$id = $this->input->post('id',null,0);
		$this->load->dmodel('team_region','team_region');
		
		$info = $this->team_region->_id_get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		$this->team_region->del("id='{$id}'");
		
		$this->add_log("删除团队地区【{$id}】",'team');
		$this->data_out(1);
		
	}
}
